package ferp.core.ai.filter;

import ferp.core.card.Card;
import ferp.core.game.Trick;
import ferp.core.player.Hand;

/**
 * User: igorgok
 * Date: 11/25/12 3:54 PM
 */
public class FilterAndroidPlaysMisere
{
  // we should get here only if android player has declared misere
  public static int apply(int candidates, int trick, int round)
  {
    if (trick == 0)
    {
      // first hand
      if (round == 0)
        for (Card.Suit s : Card.Suit.rising)
        {
          int cards = Card.Set.get(candidates, s);

          if (cards == Card.Mask.SEVEN || (cards & Card.Mask.C_78) == Card.Mask.C_78)
            return Hand.add(0, s, Card.Rank.SEVEN);
        }
    }
    else
    {
      Card highest = Trick.highest(trick, Card.Suit.NONE);
      int cards = Card.Set.get(candidates, highest.suit);

      if (cards == 0)
        for (Card.Suit s : Card.Suit.rising)
        {
          int cardsInSuit = Card.Set.get(candidates, s);

          if (cardsInSuit != 0)
            return Hand.add(0, s, Card.Set.highest(cardsInSuit));
        }

      Card.Rank lower = Card.Set.lower(cards, highest.rank);

      if (lower != null)
        return Hand.add(0, highest.suit, lower);
    }

    return candidates;
  }
}
